package org.linuxsogood.boot.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author honway.liu
 * @date 2017/8/5 下午1:35
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class Student {

    /**姓名*/
    private String name;

    /**年龄*/
    private int age;

    /**成绩*/
    private int score;

    /**参数构造*/
    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    /**空参构造*/
    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    /**main方法测试*/
    public static void main(String[] args) {
        /**
         * 1.创建10个学生对象(其中有两个学员信息完全相同)存入集合中
         * */
        Student student1 = new Student("Tom", 18, 59);
        Student student2 = new Student("Jack", 19, 59);
        Student student3 = new Student("Jackson", 24, 98);
        Student student4 = new Student("Kit", 16, 87);
        Student student5 = new Student("Adam", 20, 51);
        Student student6 = new Student("Dojo", 18, 79);
        Student student7 = new Student("Davie", 22, 60);
        Student student8 = new Student("Lee", 21, 69);
        Student student9 = new Student("Tom", 18, 59);
        Student student10 = new Student("Jerry", 21, 77);
        List<Student> students = Arrays.asList(student1, student2, student3, student4, student5, student6, student7, student8, student9, student10);

        /***
         * 2.去除重复的学员并遍历打印剩余学员信息(分别利用普通for循环、增强for循环、迭代器遍历)
         */

        // 去重
        List<String> studentToString = new ArrayList<>();

        List<Student> noRepeatStudentList = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (i != 0) {
                if (!studentToString.contains(student.toString())) {
                    noRepeatStudentList.add(student);
                }
            } else {
                noRepeatStudentList.add(student);
                studentToString.add(student.toString());
            }
        }

        System.out.println("--------普通for循环打印开始--------");
        for (int i = 0; i < noRepeatStudentList.size(); i++) {
            System.out.println(noRepeatStudentList.get(i));
        }
        System.out.println("--------普通for循环打印结束--------");


        System.out.println("--------增强for循环打印开始--------");
        for (Student student : noRepeatStudentList) {
            System.out.println(student.toString());
        }
        System.out.println("--------增强for循环打印结束--------");


        System.out.println("--------迭代器打印开始--------");
        Iterator<Student> iterator = noRepeatStudentList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("--------迭代器打印结束--------");

        /**
         * 3.打印最高分的学员信息
         */
        int maxScore = 0;
        Student maxScoreStudent = null;
        for (Student student : noRepeatStudentList) {
            if (student.getScore() > maxScore) {
                maxScore = student.getScore();
                maxScoreStudent = student;
            }
        }
        System.out.println("得分最高的学员信息如下: " + maxScoreStudent.toString());


        /**
         * 4.打印平均分
         */

        int totalScore = 0;
        for (Student student : noRepeatStudentList) {
            totalScore += student.getScore();
        }
        System.out.println("平均分为: " + (totalScore / noRepeatStudentList.size()));

        /**
         * 5.打印不及格的学员信息及数量
         */
        int passScore = 60; // 多少分算及格,这里定义了60分算是及格
        List<Student> failStudentList = new ArrayList<>();
        for (Student student : noRepeatStudentList) {
            if (student.getScore() < passScore) {
                failStudentList.add(student);
            }
        }
        if (failStudentList.size() == 0) {
            System.out.println("没有不及格的学员.");
        } else {
            System.out.println("不及格的学员有: " + failStudentList.size() + " 个.");
            for (Student student : failStudentList) {
                System.out.println(student.toString());
            }
        }
    }


}
